package org.jeecg.modules.device.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.device.entity.DeviceSummary;
import org.jeecg.modules.device.entity.TbEquipmentDetails;
import org.jeecg.modules.device.mapper.TbEquipmentDetailsMapper;
import org.jeecg.modules.device.service.ITbEquipmentDetailsService;

import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.enums.OrgLevelEnum;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 设备明细表
 * @Author: jeecg-boot
 * @Date: 2025-04-22
 * @Version: V1.0
 */
@Service
@Slf4j
public class TbEquipmentDetailsServiceImpl extends ServiceImpl<TbEquipmentDetailsMapper, TbEquipmentDetails> implements ITbEquipmentDetailsService {

    @Resource
    private TbEquipmentDetailsMapper tbEquipmentDetailsMapper;

    @Resource
    private SysDepartMapper sysDepartMapper;

    @Override
    public List<TbEquipmentDetails> statistics(TbEquipmentDetails tbEquipmentDetails, Map<String, String[]> parameterMap) {
        List<TbEquipmentDetails> finalResult = new ArrayList<>();
        String[] depIds = parameterMap.get("companyId[]");
        if (depIds != null && depIds.length > 0 && !StringUtils.isEmpty(depIds[depIds.length - 1])) {
            String depId = depIds[depIds.length - 1];
            log.info("depId:{}", depId);
            SysDepart departById = sysDepartMapper.getDepartById(depId);
            String orgCode = departById.getOrgCode();
            tbEquipmentDetails.setCode(orgCode);
        }
        List<TbEquipmentDetails> tbEquipmentDetailss = tbEquipmentDetailsMapper.selectSummerList(tbEquipmentDetails);

        Map<String, List<TbEquipmentDetails>> data = tbEquipmentDetailss.stream()
                .collect(Collectors.groupingBy(
                        TbEquipmentDetails::getCode,
                        LinkedHashMap::new, // 指定使用 LinkedHashMap
                        Collectors.toList()
                ));
        // 获取所有 key 的长度
        Set<Integer> uniqueLengths = new HashSet<>();
        for (String key : data.keySet()) {
            uniqueLengths.add(key.length());
        }

        // 将 Set 转换为数组
        int[] parentCodeLengths = uniqueLengths.stream().mapToInt(Integer::intValue).toArray();

        // 第一步 计算公司本部：对每个 key 的 List 中的收入进行汇总
        // 使用 Collectors.toMap 并指定使用 LinkedHashMap
        Map<String, DeviceSummary> summaryMap = data.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> {
                            BigDecimal sumQuantity = entry.getValue().stream()
                                    .map(TbEquipmentDetails::getBookQuantity)
                                    .filter(Objects::nonNull)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add);

                            BigDecimal sumBookNetValue = entry.getValue().stream()
                                    .map(TbEquipmentDetails::getBookNetValue)
                                    .filter(Objects::nonNull)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal sumBookOriginalValue = entry.getValue().stream()
                                    .map(TbEquipmentDetails::getBookOriginalValue)
                                    .filter(Objects::nonNull)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add);
                            TbEquipmentDetails firstAssetManagement = entry.getValue().get(0);

                            BigDecimal sumValue = sumQuantity.add(sumBookNetValue).add(sumBookNetValue).add(sumBookOriginalValue);
                            boolean flag = sumValue.compareTo(BigDecimal.ZERO) == 0;

                            String companyLevel = firstAssetManagement.getCompanyLevel();
                            if (StringUtils.isEmpty(companyLevel)) {
                                companyLevel = OrgLevelEnum.getLevelByCodeLength(firstAssetManagement.getCode().length());
                            }
                            return new DeviceSummary(
                                    firstAssetManagement.getCompanyName() + "（本部）合计",
                                    firstAssetManagement.getCode(),
                                    companyLevel,
                                    null,
                                    null,
                                    sumQuantity,
                                    sumBookNetValue,
                                    sumBookOriginalValue,
                                    flag ? new LinkedList<>() : entry.getValue()
                            );
                        },
                        (oldValue, newValue) -> oldValue, // 合并函数，这里直接返回 oldValue
                        LinkedHashMap::new // 指定使用 LinkedHashMap
                ));

/*
            List<DeviceSummary> groupByOrgCodeAndAssetTypeLevel = new ArrayList<>(tbEquipmentDetailss.stream().collect(Collectors.groupingBy(ed -> Arrays.asList(ed.getCode(), ed.getAssetTypeLevel()), Collectors.collectingAndThen(
                    Collectors.toList(), groupList -> {
                        BigDecimal bookQuantity = groupList.stream().map(TbEquipmentDetails::getBookQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal bookNetValue = groupList.stream().map(TbEquipmentDetails::getBookNetValue).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal bookOriginalValue = groupList.stream().map(TbEquipmentDetails::getBookOriginalValue).reduce(BigDecimal.ZERO, BigDecimal::add);
                        return new DeviceSummary(groupList.get(0).getCompanyName() + "（本部）合计", groupList.get(0).getCode(), groupList.get(0).getCompanyLevel(), groupList.get(0).getAssetTypeLevel(), null, bookQuantity, bookNetValue, bookOriginalValue, groupList);
                    }
            ))).values());


            List<DeviceSummary> groupByOrgCodeAndAssetTypeLevelAndAssetType = new ArrayList<>(tbEquipmentDetailss.stream().collect(Collectors.groupingBy(ed -> Arrays.asList(ed.getCode(), ed.getAssetTypeLevel(),ed.getAssetType()), Collectors.collectingAndThen(
                    Collectors.toList(), groupList -> {
                        BigDecimal bookQuantity = groupList.stream().map(TbEquipmentDetails::getBookQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal bookNetValue = groupList.stream().map(TbEquipmentDetails::getBookNetValue).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal bookOriginalValue = groupList.stream().map(TbEquipmentDetails::getBookOriginalValue).reduce(BigDecimal.ZERO, BigDecimal::add);
                        return new DeviceSummary(groupList.get(0).getCompanyName() + "（本部）", groupList.get(0).getCode(), groupList.get(0).getCompanyLevel(), groupList.get(0).getAssetTypeLevel(), groupList.get(0).getAssetType(), bookQuantity, bookNetValue, bookOriginalValue, groupList);
                    }
            ))).values());*/
/*            Map<String, Map<String,DeviceSummary>> summaryDeviceLevelMap = new LinkedHashMap<>();
            Map<String, List<DeviceSummary>> collect1 = groupByOrgCodeAndAssetTypeLevel.stream().collect(Collectors.groupingBy(DeviceSummary::getCode));
            collect1.forEach((k,v) -> {
                Map<String,DeviceSummary> deviceSummaryMap = new LinkedHashMap<>();
                v.forEach(l-> {
                    deviceSummaryMap.put(l.getAssetTypeLevel(),l);
                });
                summaryDeviceLevelMap.put(k, deviceSummaryMap);
            });

            Map<String, Map<String,Map<String,DeviceSummary>>> summaryDeviceMap = groupByOrgCodeAndAssetTypeLevelAndAssetType.stream().collect(Collectors.groupingBy(
                    DeviceSummary::getCode,
                    Collectors.groupingBy(DeviceSummary::getAssetTypeLevel,
                            Collectors.toMap(DeviceSummary::getAssetType, at -> at, (existing, replacement) -> existing))));*/
//第二部获取资产大类统计
        Map<String, Map<String, DeviceSummary>> summaryDeviceLevelMap = new LinkedHashMap<>();
        summaryMap.forEach((k, v) -> {//key为orgCode
            List<TbEquipmentDetails> motorVehicleList = v.getMotorVehicleList();
            Map<String, List<TbEquipmentDetails>> collect = motorVehicleList.stream().collect(Collectors.groupingBy(p -> Optional.ofNullable(p.getAssetTypeLevel()).orElse("")));
            Map<String, DeviceSummary> deviceSummaryMap = new LinkedHashMap<>();
            collect.forEach((q, w) -> {//key为资产大类
                if (StringUtils.isNotEmpty(q)) {
                    BigDecimal sumQuantity = w.stream()
                            .map(TbEquipmentDetails::getBookQuantity)
                            .filter(Objects::nonNull)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);

                    BigDecimal sumBookNetValue = w.stream()
                            .map(TbEquipmentDetails::getBookNetValue)
                            .filter(Objects::nonNull)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal sumBookOriginalValue = w.stream()
                            .map(TbEquipmentDetails::getBookOriginalValue)
                            .filter(Objects::nonNull)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    TbEquipmentDetails firstAssetManagement = w.get(0);
                    DeviceSummary deviceSummary = new DeviceSummary(firstAssetManagement.getCompanyName() + "（本部）合计", firstAssetManagement.getCode(), firstAssetManagement.getCompanyLevel().toString(),
                            firstAssetManagement.getAssetTypeLevel() + "(小计)", null, sumQuantity, sumBookNetValue, sumBookOriginalValue, w);

                    deviceSummaryMap.put(q, deviceSummary);
                }
            });
            summaryDeviceLevelMap.put(k, deviceSummaryMap);
        });
        //第三步，获取资产统计
        Map<String, Map<String, Map<String, DeviceSummary>>> summaryDeviceMap = new LinkedHashMap<>();
        summaryDeviceLevelMap.forEach((k, v) -> {//key为orgCode
            Map<String, Map<String, DeviceSummary>> deviceSummaryLevelMap = new LinkedHashMap<>();
            v.forEach((k1, v1) -> {//key为资产大类assetTypeLevel
                if (StringUtils.isNotEmpty(k1)) {
                    Map<String, List<TbEquipmentDetails>> deviceAssetMap = v1.getMotorVehicleList().stream().collect(Collectors.groupingBy(p -> Optional.ofNullable(p.getAssetType()).orElse("")));
                    Map<String, DeviceSummary> deviceSummaryMap = new LinkedHashMap<>();
                    deviceAssetMap.forEach((k2, v2) -> {//key为assetType资产类别
                        BigDecimal sumQuantity = v2.stream()
                                .map(TbEquipmentDetails::getBookQuantity)
                                .filter(Objects::nonNull)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal sumBookNetValue = v2.stream()
                                .map(TbEquipmentDetails::getBookNetValue)
                                .filter(Objects::nonNull)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal sumBookOriginalValue = v2.stream()
                                .map(TbEquipmentDetails::getBookOriginalValue)
                                .filter(Objects::nonNull)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        TbEquipmentDetails firstAssetManagement = v2.get(0);
                        DeviceSummary deviceSummary = new DeviceSummary(firstAssetManagement.getCompanyName() + "（本部）", firstAssetManagement.getCode(), firstAssetManagement.getCompanyLevel(),
                                firstAssetManagement.getAssetTypeLevel(), k2 + "小计", sumQuantity, sumBookNetValue, sumBookOriginalValue, v2);
                        deviceSummaryMap.put(k2, deviceSummary);
                    });
                    deviceSummaryLevelMap.put(k1, deviceSummaryMap);
                }
            });
            summaryDeviceMap.put(k, deviceSummaryLevelMap);
        });
        // 创建 currentSummaryMap 的深拷贝
        Map<String, DeviceSummary> currentSummaryMap = new LinkedHashMap<>();
        for (Map.Entry<String, DeviceSummary> entry : summaryMap.entrySet()) {
            currentSummaryMap.put(entry.getKey(), entry.getValue().deepCopy());
        }

        //创建 summaryDeviceLevelMap 的深拷贝
        Map<String, Map<String, DeviceSummary>> summaryDeviceLeveCopylMap = new LinkedHashMap<>();
        summaryDeviceLevelMap.forEach((k, v) -> {
            Map<String, DeviceSummary> deviceSummaryMap = new LinkedHashMap<>();
            v.forEach((k1, v1) -> {
                deviceSummaryMap.put(k1, v1.deepCopy());
            });
            summaryDeviceLeveCopylMap.put(k, deviceSummaryMap);
        });
        //创建 summaryDeviceMap 的深拷贝
        Map<String, Map<String, Map<String, DeviceSummary>>> summaryDeviceCopyMap = new LinkedHashMap<>();
        summaryDeviceMap.forEach((k, v) -> {
            Map<String, Map<String, DeviceSummary>> deviceSumaryMap = new LinkedHashMap<>();
            v.forEach((k1, v1) -> {
                Map<String, DeviceSummary> deviceMap = new LinkedHashMap<>();
                v1.forEach((k2, v2) -> {
                    deviceMap.put(k2, v2.deepCopy());
                });
                deviceSumaryMap.put(k1, deviceMap);
            });
            summaryDeviceCopyMap.put(k, deviceSumaryMap);
        });
        log.info("打印公司本部汇总数据 currentSummaryMap :{}", currentSummaryMap);
        for (Map.Entry<String, DeviceSummary> entry : summaryMap.entrySet()) {
            String code = entry.getKey();
            DeviceSummary summary = entry.getValue();
            aggregateToParent(summaryMap, code, summary, parentCodeLengths);
        }

        //将大类信息添加到父级中
        summaryDeviceLevelMap.forEach((k, v) -> {
            v.forEach((k1, v1) -> {
                aggregateDviLevelToParent(summaryDeviceLevelMap, k, k1, v1, parentCodeLengths);
            });
        });
        //将类别信息添加到父级中
        summaryDeviceCopyMap.forEach((k, v) -> {
            v.forEach((k1, v1) -> {
                v1.forEach((k2, v2) -> {
                    aggregateDviToParent(summaryDeviceCopyMap, k, k1, k2, v2, parentCodeLengths);
                });

            });
        });
        Map<String, DeviceSummary> containSummaryMap = new LinkedHashMap<>(summaryMap);
        for (Map.Entry<String, DeviceSummary> entry : summaryMap.entrySet()) {
            containSummaryMap.put(entry.getKey(), entry.getValue().deepCopy());
        }
        log.info("打印公司总计汇总数据 包含本部 containSummaryMap :{}", containSummaryMap);


        // 创建一个新的 DeviceSummary 对象用于汇总合计
        DeviceSummary totalSummary = getTotalSummary(currentSummaryMap);
        TbEquipmentDetails totalAssetManagement = new TbEquipmentDetails();
        BeanUtils.copyProperties(totalSummary, totalAssetManagement);
        finalResult.add(totalAssetManagement);

        // 第三步：将汇总结果和原始数据合并到一个 List 中
        for (DeviceSummary amount : containSummaryMap.values()) {

            TbEquipmentDetails amountAssetManagement = new TbEquipmentDetails();
            BeanUtils.copyProperties(amount, amountAssetManagement);
//                amountAssetManagement.setCode(amount.getOrgCode());
            finalResult.add(amountAssetManagement); //添加总计

            TbEquipmentDetails currentAssetManagement = new TbEquipmentDetails();
            DeviceSummary current = currentSummaryMap.get(amount.getCode());
            if (!amountAssetManagement.getCompanyName().equals(current.getCompanyName())) {
                BeanUtils.copyProperties(current, currentAssetManagement);
                finalResult.add(currentAssetManagement);//添加本部
            }
            //添加大类信息和类别信息
            String amOrgCode = amount.getCode();

            Map<String, DeviceSummary> deviceSummaryLevelMap = summaryDeviceLeveCopylMap.get(amOrgCode);
            deviceSummaryLevelMap.forEach((k, v) -> {
                TbEquipmentDetails currentAssetLevelManagement = new TbEquipmentDetails();
                BeanUtils.copyProperties(v, currentAssetLevelManagement);
//                    v.setOrgCode(amount.getOrgCode());
                finalResult.add(currentAssetLevelManagement);//添加本部
                Map<String, DeviceSummary> deviceSummaryMap = summaryDeviceMap.get(amOrgCode).get(k);
                deviceSummaryMap.forEach((k1, v1) -> {
                    TbEquipmentDetails currentAssetDeviceManagement = new TbEquipmentDetails();
                    BeanUtils.copyProperties(v1, currentAssetDeviceManagement);
//                        currentAssetDeviceManagement.setCode(v1.getOrgCode());
                    finalResult.add(currentAssetDeviceManagement);//添加本部
                });
            });

            finalResult.addAll(current.getMotorVehicleList()); //添加本部计算的原始数据
        }
        return finalResult;
    }

    //全部汇总
    @NotNull
    private static DeviceSummary getTotalSummary(Map<String, DeviceSummary> currentSummaryMap) {
        DeviceSummary totalSummary = new DeviceSummary(
                "汇总合计", // 公司名称
                "", // 公司代码
                "", // 公司级次
                "",//资产大类
                "",//资产类别
                BigDecimal.ZERO, // 账面数-面积
                BigDecimal.ZERO, // 账面数-原值
                BigDecimal.ZERO, // 账面数-净值
                new ArrayList<>() // 资产列表
        );
        // 遍历 currentSummaryMap，将所有值汇总到 totalSummary
        for (DeviceSummary summary : currentSummaryMap.values()) {
            totalSummary.addBookQuantity(summary.getBookQuantity());
            totalSummary.addBookNetValue(summary.getBookNetValue());
            totalSummary.addBookOriginalValue(summary.getBookOriginalValue());
//            totalSummary.setOrgCode(summary.get());
        }
        return totalSummary;
    }

    // 定义一个方法，用于将子公司的数据加和到父公司
    private static void aggregateToParent(Map<String, DeviceSummary> summaryMap, String code, DeviceSummary summary, int[] parentCodeLengths) {
        for (int length : parentCodeLengths) {
            if (code.length() > length) {
                String parentCode = code.substring(0, length); // 获取父公司的代码
                if (summaryMap.containsKey(parentCode)) {
                    // 将子公司的数据加和到父公司
                    DeviceSummary parentSummary = summaryMap.get(parentCode);
                    parentSummary.addBookQuantity(summary.getBookQuantity());
                    parentSummary.addBookNetValue(summary.getBookNetValue());
                    parentSummary.addBookOriginalValue(summary.getBookOriginalValue());
//                    parentSummary.setCode(summary.getCode());
                    // 更新公司名称
                    String originalName = parentSummary.getCompanyName();
                    parentSummary.setCompanyName(originalName.replace("（本部）合计", "（公司）总计"));
                }
            }
        }
    }

    /**
     * 用于将子公司的资产大类数据加和到父公司
     *
     * @param code              企业编码
     * @param assetTypeLevel    资产大类
     * @param summary
     * @param parentCodeLengths
     * @param summaryMap
     */
    private static void aggregateDviLevelToParent(Map<String, Map<String, DeviceSummary>> summaryMap, String code, String assetTypeLevel, DeviceSummary summary, int[] parentCodeLengths) {
        for (int length : parentCodeLengths) {
            if (code.length() > length) {
                String parentCode = code.substring(0, length); // 获取父公司的代码
                if (summaryMap.containsKey(parentCode)) {
                    DeviceSummary parentSummary = summaryMap.get(parentCode).get(assetTypeLevel);
                    if (parentSummary == null) {
                        DeviceSummary parentSummaryNew = new DeviceSummary();
                        summaryMap.get(parentCode).entrySet().stream().findFirst().ifPresent(entry -> BeanUtils.copyProperties(entry.getValue(), parentSummaryNew));
//                        BeanUtils.copyProperties(summary,deviceSummary);
                        parentSummaryNew.setAssetTypeLevel(assetTypeLevel);
                        parentSummaryNew.setAssetType(null);
                        parentSummaryNew.setBookQuantity(new BigDecimal("0.0"));
                        parentSummaryNew.setBookNetValue(new BigDecimal("0.0"));
                        parentSummaryNew.setBookOriginalValue(new BigDecimal("0.0"));
                        summaryMap.get(parentCode).put(assetTypeLevel, parentSummaryNew);
                        parentSummary = parentSummaryNew;

                    }
                    // 将子公司的数据加和到父公司
                    parentSummary.addBookQuantity(summary.getBookQuantity());
                    parentSummary.addBookNetValue(summary.getBookNetValue());
                    parentSummary.addBookOriginalValue(summary.getBookOriginalValue());
//                    parentSummary.setOrgCode(summary.getOrgCode());
                    // 更新公司名称
                    String originalName = parentSummary.getCompanyName();
                    if (StringUtils.isNotEmpty(originalName)) {
                        parentSummary.setCompanyName(originalName.replace("（本部）合计", "（公司）总计"));
                    }
                }
            }
        }
    }

    /**
     * 定义一个方法，用于将子公司的数据加和到父公司
     *
     * @param code              企业编码
     * @param assetTypeLevel    资产大类
     * @param assetType         资产类别
     * @param summary
     * @param parentCodeLengths
     * @param summaryMap
     */
    private static void aggregateDviToParent(Map<String, Map<String, Map<String, DeviceSummary>>> summaryMap, String code, String assetTypeLevel, String assetType, DeviceSummary summary, int[] parentCodeLengths) {
        for (int length : parentCodeLengths) {
            if (code.length() > length) {
                String parentCode = code.substring(0, length); // 获取父公司的代码
                if (summaryMap.containsKey(parentCode)) {
                    Map<String, DeviceSummary> stringDeviceSummaryMap = summaryMap.get(parentCode).get(assetTypeLevel);
                    if (stringDeviceSummaryMap == null) {

                        Map<String, DeviceSummary> deviceSummaryMap = new HashMap<>();
                        DeviceSummary deviceSummary = new DeviceSummary();
                        Map<String, Map<String, DeviceSummary>> stringMapMap = summaryMap.get(parentCode);
                        stringMapMap.entrySet().stream().findFirst().ifPresent(entry -> entry.getValue().entrySet().stream().findFirst().ifPresent(entry1 -> BeanUtils.copyProperties(entry1.getValue(), deviceSummary)));
//                        BeanUtils.copyProperties(summary,deviceSummary);
                        deviceSummary.setAssetTypeLevel(assetTypeLevel);
                        deviceSummary.setAssetType(assetType);
                        deviceSummary.setBookQuantity(new BigDecimal("0.0"));
                        deviceSummary.setBookNetValue(new BigDecimal("0.0"));
                        deviceSummary.setBookOriginalValue(new BigDecimal("0.0"));
                        deviceSummaryMap.put(assetType, deviceSummary);

                        summaryMap.get(parentCode).put(assetTypeLevel, deviceSummaryMap);
                    }

                    DeviceSummary parentSummary = summaryMap.get(parentCode).get(assetTypeLevel).get(assetType);
                    if (parentSummary == null) {
                        DeviceSummary deviceSummary = new DeviceSummary();
                        summaryMap.get(parentCode).get(assetTypeLevel).put(assetType, deviceSummary);
                        summaryMap.get(parentCode).get(assetTypeLevel).entrySet().stream().findFirst().ifPresent(entry -> BeanUtils.copyProperties(entry.getValue(), deviceSummary));
                        parentSummary = deviceSummary;
                        parentSummary.setAssetTypeLevel(assetTypeLevel);
                        parentSummary.setAssetType(assetType);
                        parentSummary.setBookQuantity(new BigDecimal("0.0"));
                        parentSummary.setBookNetValue(new BigDecimal("0.0"));
                        parentSummary.setBookOriginalValue(new BigDecimal("0.0"));
                    }
                    // 将子公司的数据加和到父公司
                    parentSummary.addBookQuantity(summary.getBookQuantity());
                    parentSummary.addBookNetValue(summary.getBookNetValue());
                    parentSummary.addBookOriginalValue(summary.getBookOriginalValue());
//                    parentSummary.setOrgCode(summary.getOrgCode());
                    // 更新公司名称
                    String originalName = parentSummary.getCompanyName();
                    if (StringUtils.isNotEmpty(originalName)) {
                        parentSummary.setCompanyName(originalName.replace("（本部）合计", "（公司）总计"));
                    }
                }
            }
        }
    }

}
